package com.vitabuybuy.tv.ui.mine.order.after

import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityEditAddressBinding
import com.vitabuybuy.tv.dialog.CategroyPopup
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.toast.XToast
import com.vitabuybuy.tv.ui.mine.order.after.viewmodel.AfterSaleViewModel
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_edit_logistics.*
import org.greenrobot.eventbus.EventBus

class EditLogisticsActivity : BaseActivity<AfterSaleViewModel, ActivityEditAddressBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_edit_logistics
    private var orderId :Int?= -1

    override fun onGetBundle(bundle: Bundle?) {
        orderId = bundle?.getInt("orderId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.after_sale_detail))
        mTvRight.apply {
            visibility = View.VISIBLE
            text = getString(R.string.submit)
        }
    }

    override fun observeData() {
        viewModel.firstCategorylist.observe(this, Observer {
            CategroyPopup(mContext, it) { dto ->
                tvDeliverCompany.text = dto.name
            }.showPopupWindow()
        })
    }

    override fun eventHandling() {
        mTvRight.setOnClickListener {
            when {
                tvDeliverCompany.text.isNullOrEmpty() -> XToast.normal(getString(R.string.please_choose_logistics_company))
                etRefundOrderNumber.text.isNullOrEmpty() -> XToast.normal(getString(R.string.pls_enter_logistics_number))
                else -> {
                    viewModel.haveSend(
                        orderId, tvDeliverCompany.text.toString(),
                        etRefundOrderNumber.text.toString(),
                        etRefundOrderExplanation.text.toString()
                    )
                }
            }
        }
        tvDeliverCompany.setOnClickListener {
            viewModel.getLogisticsCompany()
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            3 -> {
                XToast.success(getString(R.string.pls_wait_for_bussiness_deal))
                EventBus.getDefault().post(MessageEvent(EventCode.update_after_sale_order_detail))
                finish()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, orderId: Int?) {
            mContext.startActivity(
                bundleOf(Pair("orderId", orderId)),
                EditLogisticsActivity::class.java
            )
        }
    }
}
